package com.huoyun.user.dao;

import java.util.HashMap;
import java.util.Map;

import org.apache.commons.lang3.ArrayUtils;

import com.huoyun.base.dao.BaseDaoImpl;
import com.huoyun.user.bean.User;

public class UserDaoImpl extends BaseDaoImpl<User> implements UserDao {

	@Override
	public void updateMobilePhoneByUserId(long userId, String mobilePhone) {
		Map<String, Object> dataMap = new HashMap<String, Object>();
		dataMap.put("userId", userId);
		dataMap.put("mobilePhone", mobilePhone);

		sqlSessionTemplate.update("com.huoyun.user.dao.UserDaoMapper.updateMobilePhoneByUserId", dataMap);
	}

	@Override
	public void updateEmailByUserId(long userId, String email) {
		Map<String, Object> dataMap = new HashMap<String, Object>();
		dataMap.put("userId", userId);
		dataMap.put("email", email);

		sqlSessionTemplate.update("com.huoyun.user.dao.UserDaoMapper.updateEmailByUserId", dataMap);
	}

	@Override
	public void updateRealnameByUserId(long userId, String realname) {
		Map<String, Object> dataMap = new HashMap<String, Object>();
		dataMap.put("userId", userId);
		dataMap.put("realname", realname);

		sqlSessionTemplate.update("com.huoyun.user.dao.UserDaoMapper.updateRealnameByUserId", dataMap);
	}

	@Override
	public void updateBankAccountByUserId(long userId, String bankAccount) {
		// Map<String, Object> dataMap = new HashMap<String, Object>();
		// dataMap.put("userId", userId);
		// dataMap.put("bankAccount", bankAccount);
		//
		// sqlSessionTemplate.update("com.huoyun.user.dao.UserDaoMapper.updateBankAccountByUserId",
		// dataMap);
	}

	public User getUserByUsername(String username) {
		return sqlSessionTemplate.selectOne("com.huoyun.user.dao.UserDaoMapper.getUserByUsername", username);
	}

	@Override
	public long saveUser(User user) {
		sqlSessionTemplate.insert("com.huoyun.user.dao.UserDaoMapper.saveUser", user);

		return user.getId();
	}

	@Override
	public User getUserById(long userId) {
		return sqlSessionTemplate.selectOne("com.huoyun.user.dao.UserDaoMapper.getUserById", userId);
	}

	@Override
	public void changePassword(long userId, String newPassword) {
		Map<String, Object> dataMap = new HashMap<String, Object>();
		dataMap.put("userId", userId);
		dataMap.put("newPassword", newPassword);

		sqlSessionTemplate.update("com.huoyun.user.dao.UserDaoMapper.changePassword", dataMap);
	}

	@Override
	public void updateAvatarByUserId(long userId, String avatarUrl) {
		Map<String, Object> dataMap = new HashMap<String, Object>();
		dataMap.put("userId", userId);
		dataMap.put("avatarUrl", avatarUrl);

		sqlSessionTemplate.update("com.huoyun.user.dao.UserDaoMapper.updateAvatarByUserId", dataMap);
	}

	@Override
	public void userRelationGroup(long userId, int[] groupIds) {
		if (ArrayUtils.isNotEmpty(groupIds)) {
			for (int groupId : groupIds) {
				Map<String, Object> dataMap = new HashMap<String, Object>();
				dataMap.put("userId", userId);
				dataMap.put("groupId", groupId);
				sqlSessionTemplate.insert("com.huoyun.user.dao.UserDaoMapper.userRelationGroup", dataMap);
			}
		}
	}

	@Override
	public void upateSalt(long userId, String salt) {
		// TODO Auto-generated method stub
		Map<String, Object> dataMap = new HashMap<String, Object>();
		dataMap.put("userId", userId);
		dataMap.put("salt", salt);

		sqlSessionTemplate.update("com.huoyun.user.dao.UserDaoMapper.updateSalt", dataMap);
	}

	@Override
	public void updateCredit(long credit, long id) {
		// TODO Auto-generated method stub
		Map<String, Object> dataMap = new HashMap<String, Object>();
		dataMap.put("credit", credit);
		dataMap.put("userId", id);
		sqlSessionTemplate.update("com.huoyun.user.dao.UserDaoMapper.changeCredit", dataMap);

	}


	@Override
	public void changeSecurePassword(long userId, String newSecurePassword) {
		Map<String, Object> dataMap = new HashMap<String, Object>();
		dataMap.put("userId", userId);
		dataMap.put("newSecurePassword", newSecurePassword);
		
		sqlSessionTemplate.update("com.huoyun.user.dao.UserDaoMapper.changeSecurePassword", dataMap);
	}
	
}
